package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.query.DestinationQuery;
import cn.wolfcode.luowowo.article.query.DestinationSearchQueryObject;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IRegionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationMapper destinationMapper;

    @Autowired
    private IRegionService regionService;


    @Override
    public List <Destination> getDestsByDeep(int deep) {
        return destinationMapper.selectDestsByDeep( deep );
    }

    @Override
    public void saveOrUpdate(Destination destination) {

    }

    @Override
    public List <Destination> getDestByRegionId(Long rid) {

        //rid ==-1  表示查询国内所有省份

        if (rid == -1) {
            //id = 1; 表示中国id
            return destinationMapper.selectSubDestParentId( 1L );
        }


        //区域
        Region region = regionService.get( rid );
        //关联的目的地id
        Long[] destIds = region.getRefIds();

        return destinationMapper.selectDestByIds( destIds );
    }


    @Override
    public PageInfo query(DestinationQuery qo) {
        PageHelper.startPage( qo.getCurrentPage(), qo.getPageSize() );
        return new PageInfo( destinationMapper.selectForList( qo ) );
    }

    @Override
    public List <Destination> getToasts(Long parentId) {
        //parentId: 广州
        //: 根 >> 中国  >> 广东 >> 广州
        List <Destination> list = new ArrayList <>();

        createToast( parentId, list );
        Collections.reverse( list );  //列表反序
        return list;
    }

    //创建吐司
    private void createToast(Long parentId, List <Destination> list) {
        //广州
        Destination destination = destinationMapper.selectByPrimaryKey( parentId );
        if (destination == null) {
            return;
        }

        list.add( destination );

        if (destination.getParent() != null) {
            createToast( destination.getParent().getId(), list );
        }

    }

    @Override
    public Destination getCountry(Long id) {
        List <Destination> list = this.getToasts( id );

        if (list != null && list.size() > 0) {
            return list.get( 0 );
        }
        return null;
    }

    @Override
    public Destination getProvince(Long id) {

        //必须是国内的才设置省份

        List <Destination> list = this.getToasts( id );
        if (list != null && list.size() > 0) {
            //国外
            if (list.get( 0 ).getId() != 1) {
                return null;
            }
        }

        if (list.size() > 1) {
            return list.get( 1 );
        }

       /* Destination destination = destinationMapper.selectByPrimaryKey(id);

        //国家
        if(destination.getDeep() == 1){
            return null;
        }

        //省份
        if (destination.getDeep() == 2){
            return destination;
        }


        //城市
        if (destination.getDeep() == 3){
            return destination.getParent();
        }*/


        return null;
    }

    @Override
    public List <Destination> list() {
        return destinationMapper.selectAll();
    }

    @Override
    public void updateHot(Long id, boolean ishot) {
        destinationMapper.updateHot( id, ishot );
    }

    @Override
    public List <Destination> queryHotDestByRegionId(Long regionId) {
        if (regionId == -1) {
            //id = 1; 表示中国id
            return destinationMapper.selectSubDestParentId( 1L );
        }
        //区域
        Region region = regionService.get( regionId );
        //关联的目的地id
        Long[] destIds = region.getRefIds();
        List <Destination> list = destinationMapper.queryHotDestByDestIds( destIds );
        return list;

    }

    @Override
    public PageInfo queryForListAccordingCondition(DestinationSearchQueryObject qo) {
        PageHelper.startPage( qo.getCurrentPage(), qo.getPageSize() );
        return  new PageInfo( destinationMapper.queryForListAccordingCondition(qo));
    }

    @Override
    public List<Destination> selectDestByMouthId(Long regionId) {

        return destinationMapper.selectDestByMouthId(regionId);
    }

    @Override
    public List<Destination> selectDestByThemeId(Long regionId) {

        return destinationMapper.selectDestByThemeId(regionId);
    }
}
